﻿Imports System.Data
Partial Class KYC_ApproveHistory
    Inherits Web.UI.BasePage

#Region "Valiable"
    Dim ds As DataSet

    Dim clsDataTable As New clsLINQToDataTable
    Dim convertDate As clsDateFormat
    Dim clsSessionMenuRight As clsSessionMenuRight = New clsSessionMenuRight
    Dim clsDatabase As clsDatabase
   
#End Region

#Region "Even Page"
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            clsSessionMenuRight = Session("clsMenuRight")
            btnSearch.Attributes.Add("onclick", "NoDoubleSubmit();")

            If Not IsPostBack Then
                chkIsLogin()
                ds = New DataSet()
                CreateControlMP()
                convertDate = New clsDateFormat
                txtDateStart.Text = convertDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.Th, Now.Date)
                txtDateEnd.Text = convertDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.Th, Now.Date)
                convertDate = Nothing

            Else
                ds = Session("ds")
            End If
        Catch ex As Exception
            ScriptManager.RegisterStartupScript(UpdatePanel1, _
                                                            UpdatePanel1.GetType(), _
                                                            "clientScript", _
                                                            "alert('" + ex.Message.Replace("'", "") + "')", _
                                                            True)
        End Try
    End Sub

    Protected Sub Page_Unload(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Unload
        Session("ds") = ds
    End Sub
#End Region

#Region "SetForm"

    Sub CreateControlMP()
        Dim page As Label = CType(Master.FindControl("lblPage"), Label)
        page.Text = "สรุปรายงานการอนุมัติ"
    End Sub

#End Region


#Region "Even GridView"

    Dim intBeforeItemIndex As Integer

    Protected Sub grdIndividual_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles grdIndividual.PageIndexChanging

        grdIndividual.PageIndex = e.NewPageIndex
        grdIndividual.SelectedIndex = -1
        grdIndividual.DataSource = ds.Tables("dsCustomerHis")
        grdIndividual.DataBind()
    End Sub
    Protected Sub grdIndividual_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdIndividual.RowDataBound

        If (e.Row.RowType.ToString = "DataRow") Then
            Dim lblUpdateDate As Label = CType(e.Row.FindControl("lblUpdateDate"), Label)
            If Not (lblUpdateDate.Text = "") Then
                convertDate = New clsDateFormat
                lblUpdateDate.Text = convertDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.Th, lblUpdateDate.Text)
                convertDate = Nothing
            End If
        End If

    End Sub
    Protected Sub grdIndividual_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles grdIndividual.SelectedIndexChanged

        If (grdIndividual.SelectedIndex = intBeforeItemIndex) Then
            grdIndividual.SelectedIndex = -1
        End If

    End Sub
    Protected Sub grdIndividual_SelectedIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSelectEventArgs) Handles grdIndividual.SelectedIndexChanging

        intBeforeItemIndex = grdIndividual.SelectedIndex

    End Sub

    Protected Sub grdNonIndividual_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles grdNonIndividual.PageIndexChanging
        grdNonIndividual.PageIndex = e.NewPageIndex
        grdNonIndividual.SelectedIndex = -1
        grdNonIndividual.DataSource = ds.Tables("dsCustomerHis")
        grdNonIndividual.DataBind()
    End Sub

    Protected Sub grdNonIndividual_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdNonIndividual.RowDataBound
        If (e.Row.RowType.ToString = "DataRow") Then
            Dim lblUpdateDate As Label = CType(e.Row.FindControl("lblUpdateDate"), Label)
            If Not (lblUpdateDate.Text = "") Then
                convertDate = New clsDateFormat
                lblUpdateDate.Text = convertDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.Th, lblUpdateDate.Text)
                convertDate = Nothing
            End If
        End If
    End Sub

    Protected Sub grdnonIndividual_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles grdNonIndividual.SelectedIndexChanged
        If (grdNonIndividual.SelectedIndex = intBeforeItemIndex) Then
            grdNonIndividual.SelectedIndex = -1

        End If

    End Sub

    Protected Sub grdnonIndividual_SelectedIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSelectEventArgs) Handles grdNonIndividual.SelectedIndexChanging
        intBeforeItemIndex = grdNonIndividual.SelectedIndex

    End Sub


#End Region

#Region "Button"

    Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        Try
            If ddlType.SelectedIndex = 0 Then
                ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", "alert('กรุณาเลือกประเภทลูกค้า')", True)
            Else
                grdIndividual.DataSource = Nothing
                grdIndividual.DataBind()
                grdNonIndividual.DataSource = Nothing
                grdNonIndividual.DataBind()
                IsDataBind()
            End If
        Catch ex As Exception
            ScriptManager.RegisterStartupScript(UpdatePanel1, _
                                                            UpdatePanel1.GetType(), _
                                                            "clientScript", _
                                                            "alert('" + ex.Message.Replace("'", "") + "')", _
                                                            True)
        End Try
    End Sub

    Protected Sub btnPrint_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrint.Click
        Try
            If ddlType.SelectedIndex = 0 Then
                ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", "alert('กรุณาเลือกประเภทลูกค้า')", True)
            Else

                'Dim strConvertDateStart As String
                'Dim strConvertDateEnd As String

                'Dim tempStart() As String = txtDateStart.Text.Split("/")
                'Dim tempEnd() As String = txtDateEnd.Text.Split("/")

                'If (tempStart(2) > 2500) Then
                '    strConvertDateStart = tempStart(0) & "/" & tempStart(1) & "/" & tempStart(2) - 543

                'Else
                '    strConvertDateStart = txtDateStart.Text
                'End If

                'If (tempEnd(2) > 2500) Then
                '    strConvertDateEnd = tempEnd(0) & "/" & tempEnd(1) & "/" & tempEnd(2) - 543
                'Else
                '    strConvertDateEnd = txtDateEnd.Text
                'End If


                If Not ddlType.SelectedIndex = 0 Then
                    Dim param As String = "{ 'StatusPint' : '" & ddlType.SelectedValue & "'," _
                                         & "'CUSTTYPE' : '" & ddlType.SelectedValue & "/" & ddlType.SelectedItem.Text & "'," _
                                         & "'STARTDATE' : '" & txtDateStart.Text & "'," _
                                         & "'ENDDATE' : '" & txtDateEnd.Text & "'}"
                    '
                    '    ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType, "key", _
                    '                   "OpenWindowWithPost('../Form/RptApprove.aspx','width=800,height=600,left=100,top=100,resizable=yes,scrollbars=yes','Report', " + param + ");", True)
                    'ElseIf ddlType.SelectedValue = 2 Then
                    ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType, "key", _
                           "OpenWindowWithPost('../Form/frmPrintRptApprove.aspx','width=800,height=600,left=100,top=100,resizable=yes,scrollbars=yes','Report', " + param + ");", True)
                Else
                    ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", "alert('กรุณาเลือกประเภทลูกค้า')", True)
                End If

            End If

        Catch ex As Exception
            ScriptManager.RegisterStartupScript(UpdatePanel1, _
                                                            UpdatePanel1.GetType(), _
                                                            "clientScript", _
                                                            "alert('" + ex.Message.Replace("'", "") + "')", _
                                                            True)
        End Try
    End Sub

#End Region

#Region "Function"

    Private Sub IsDataBind()
        Try
            Dim cltParameter As New Collection
            Dim clsVariable As clsVariable = Nothing
            Dim clsDate As New clsDateFormat
            clsDatabase = New clsDatabase

            Dim sql As String = "select l.*,c.THFIRSTNAME,c.THMIDNAME,c.THLASTNAME," _
                                & " c.ENFIRSTNAME, c.ENMIDNAME, c.ENLASTNAME," _
                                & " c.CUSTTYPE, c.UNIQUEID, c.CUSTCODE," _
                                & " c.AMLCDD, c.AMLREASON, c.AMLCDDOVERVIEW, c.AMLREASONOVERVIEW, " _
                                & " s.APPROVENAME APPROVESTATUS" _
                                & " from " & System.Configuration.ConfigurationSettings.AppSettings("TableLog_Approve") & " l " _
                                & " left join " & System.Configuration.ConfigurationSettings.AppSettings("TableMCUSTOMER") & " c on l.CUSTSEQ = c.CUSTSEQ " _
                                & " left join " & System.Configuration.ConfigurationSettings.AppSettings("TableMAPPROVESTATUS") & " s on l.ApproveCode=s.ApproveCode " _
                                & " where c.CUSTTYPE = :prmCUSTTYPE   " _
                                & " and l.APPROVENAME = :prmAPPROVENO " _
                                & " and l.APPROVEDATE >= :prmAPPROVEDATEStart and l.APPROVEDATE <= :prmAPPROVEDATEEnd " _
                                & " order by l.APPROVEDATE desc "

            clsVariable = New clsVariable
            clsVariable.nameColumn = "prmCUSTTYPE"
            clsVariable.valueColumn = ddlType.SelectedValue
            cltParameter.Add(clsVariable)

            clsVariable = New clsVariable
            clsVariable.nameColumn = "prmAPPROVENO"
            clsVariable.valueColumn = clsSessionMenuRight.getUserName
            cltParameter.Add(clsVariable)

            Dim aDate As Date = clsDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, strDateIsChk:=txtDateStart.Text)
            Dim bDate As Date = clsDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, strDateIsChk:=txtDateEnd.Text)
            clsVariable = New clsVariable
            clsVariable.nameColumn = "prmAPPROVEDATEStart"
            clsVariable.valueColumn = aDate
            cltParameter.Add(clsVariable)

            clsVariable = New clsVariable
            clsVariable.nameColumn = "prmAPPROVEDATEEnd"
            clsVariable.valueColumn = bDate
            cltParameter.Add(clsVariable)


            If ds.Tables.Contains("dsCustomerHis") Then
                ds.Tables.Remove("dsCustomerHis")
            End If

            ds = clsDatabase.QueryDataSet(sql, cltParameter, "dsCustomerHis")

            'ds.Tables.Add(clsDatabase.QueryDataSet(sql, "dsCustomerHis").Tables("dsCustomerHis").Copy)


            If ddlType.SelectedValue = 1 Then
                grdIndividual.DataSource = ds.Tables("dsCustomerHis")
                grdIndividual.DataBind()

                If grdIndividual.Rows.Count = 0 Then
                    lblMsg.Visible = True
                    lblTotal.Text = ""
                Else
                    lblMsg.Visible = False
                    lblTotal.Text = "จำนวนรายการทั้งหมด " + ds.Tables("dsCustomerHis").Rows.Count.ToString + " รายการ "
                End If

            Else
                grdNonIndividual.DataSource = ds.Tables("dsCustomerHis")
                grdNonIndividual.DataBind()

                If grdNonIndividual.Rows.Count = 0 Then
                    lblMsg.Visible = True
                    lblTotal.Text = ""
                Else
                    lblMsg.Visible = False
                    lblTotal.Text = "จำนวนรายการทั้งหมด " + ds.Tables("dsCustomerHis").Rows.Count.ToString + " รายการ "
                End If
            End If

            'If grdIndividual.Rows.Count = 0 Then
            '    lblMsg.Visible = True
            '    lblTotal.Text = ""
            'Else
            '    lblMsg.Visible = False
            '    lblTotal.Text = "จำนวนรายการทั้งหมด " + ds.Tables("dsCustomerHis").Rows.Count.ToString + " รายการ "
            'End If

            clsDatabase = Nothing

        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Private Sub SaveLogPrint(ByVal index As Integer)
        Try
            Dim program As String = "M43"

            Dim desc As String = "พิมพ์รายงานสรุปการอนุมัติ ตั้งแต่วันที่ " + txtDateStart.Text + " ถึงวันที่ " + txtDateEnd.Text

            Dim clsLogTransaction As New clsLogTransaction
            clsLogTransaction.saveLogTransaction("", clsSessionMenuRight.getUserName, 2, desc, program)
            clsLogTransaction = Nothing

        Catch ex As Exception
            Throw ex
        End Try
    End Sub

#End Region

End Class
